/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mscs.server.admin.logic;

import java.util.ArrayList;
import java.util.List;
import mscs.data.IFileDataStore;
import mscs.data.IPersonProxy;
import mscs.data.pojo.Person;

/**
 *
 * @author Administrator
 */
public class AddPersonUtil  implements IAddPersonUtil{
    private ArrayList<Person> newList = new ArrayList<Person>();
    private IFileDataStore fds = new IPersonProxy();
    
    
    public AddPersonUtil(){
        
    }

    public List<Person> getNewList() {
        return this.newList;
    }

    public boolean addToNewList(Person person) {
        if(this.isValidId(person)){
           this.newList.add(person);
           return true;
        }else{
            return false;
        }
    }

    public boolean setByIndex(int index, Person person) {
        if(this.isValidId(person)){
            this.newList.set(index, person);
            return true;
        }else{
            return false;
        }
    }
    
    private boolean isValidId(Person person){
        if(!this.fds.searchObjectsByAttr("id", String.valueOf(person.getId())).isEmpty())
            return false;
        for(Person p: newList){
            if(p.getId() == person.getId()){
                return false;
            }
        }
        return true;
    }

    public Person getByIndex(int index) {
        return this.newList.get(index);
    }
    
    public void removeFromNewList(int index) {
        this.newList.remove(index);
    }

    public boolean commitNewList() {
        if(this.newList.isEmpty()){
            return false;
        }else{
            this.fds.add(newList);
            newList = new ArrayList<Person>();
            return true;
        }
    }
    
}
